﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyD
{
    class Cryptography
    {
        protected internal int[] table = { 0x1, 0x3, 0xE, 0xE, 0xFE, 0x32, 0x12, 0x16 };
        private string[] numbers = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        private string[] badStrs = { ":", "\n", "\r" };
        private string[] badRepl = { "badrepl1", "badrepl2", "badrepl3" };

        public string Encrypt(string text)
        {
            string _return = "";

            for (int i = 0; i < numbers.Length; i++)
                text = text.Replace(numbers[i], ":" + numbers[i] + ":");

            int table_len = table.Length - 1;
            int table_pos = 0;

            for (int i = 0; i < text.Length; i++)
            {
                _return += Convert.ToChar(text[i] + table[table_pos]).ToString();

                table_pos++;

                if (table_pos > table_len)
                    table_pos = 0;
            }

            for (int i = 0; i < badStrs.Length; i++)
                _return = _return.Replace(badStrs[i], badRepl[i]);

            return _return;
        }

        public string Decrypt(string text)
        {
            for (int i = 0; i < badRepl.Length; i++)
                text = text.Replace(badRepl[i], badStrs[i]);

            string _return = "";

            int table_len = table.Length - 1;
            int table_pos = 0;

            for (int i = 0; i < text.Length; i++)
            {
                _return += Convert.ToChar(text[i] - table[table_pos]).ToString();

                table_pos++;

                if (table_pos > table_len)
                    table_pos = 0;
            }

            for (int i = 0; i < numbers.Length; i++)
                _return = _return.Replace(":" + numbers[i] + ":", numbers[i]);

            return _return;
        }
    }
}
